import time

from selenium import webdriver

from ..exception.TestException import TestException
from ..common.ui.Selector import Selector
from . import Grid
import pytest


def test_tree_select_grid():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/grid/index.html")
    time.sleep(2)
    try:
        multi_tree_grid = browser.find_element_by_id('treeGrid.html')
        browser.execute_script('$(arguments[0]).click()', multi_tree_grid)
        treeGrid = Grid(browser, 'grid01', Selector.CLASS_NAME)
        time.sleep(1)
        treeGrid.setValue("ZTP流程_勿删", "Cooperation3 [P100183/1.0]")
        treeGrid.expand_all()
    finally:
        browser.close()


def test_tree_multiple_select_grid():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/grid/index.html")
    time.sleep(2)
    try:
        multi_tree_grid = browser.find_element_by_id('multiTreeGrid.html')
        browser.execute_script('$(arguments[0]).click()', multi_tree_grid)
        treeGrid = Grid(browser, 'grid01', Selector.CLASS_NAME)
        time.sleep(1)
        treeGrid.setValue("彬哥专属目录", "复杂网关 [P100295/1.0]")
        treeGrid.setValue("导入导出")
        treeGrid.expand_all()
    finally:
        browser.close()


def test_select_line():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/grid/index.html")
    time.sleep(2)
    try:
        grid = Grid(browser, 'gridId', Selector.ID)
        grid.setValue('Inv No', '12')
    finally:
        browser.close()


def test_multiselect_line():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/grid/index.html")
    time.sleep(2)
    try:
        multi_tree_grid = browser.find_element_by_id('multiSelect.html')
        browser.execute_script('$(arguments[0]).click()', multi_tree_grid)
        grid = Grid(browser, 'grid01', Selector.ID)
        grid.setValue('id', '29')
        grid.setValue('id', '28')
    finally:
        browser.close()

def test_tree_select_line():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/grid/index.html")
    time.sleep(2)
    try:
        treeGridPage = browser.find_element_by_id('treeGridPage.html')
        browser.execute_script('$(arguments[0]).click()', treeGridPage)
        grid = Grid(browser, 'grid02', Selector.ID)
        grid.setValue('id', '4', '5')
    finally:
        browser.close()

def test_tree_select_line_with_lazy_load():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/grid/index.html")
    time.sleep(2)
    try:
        treeGridPage = browser.find_element_by_id('treeGridPage.html')
        browser.execute_script('$(arguments[0]).click()', treeGridPage)
        grid = Grid(browser, 'grid02', Selector.ID)
        # id =3 是lazyload模式，需要点击下拉
        grid.setValue('id', '3', 'jqg1','jqg3', 'jqg6')
    finally:
        browser.close()


def test_tree_select_line_without_set_col_label():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/grid/index.html")
    time.sleep(2)
    try:
        treeGridPage = browser.find_element_by_id('treeGridPage.html')
        browser.execute_script('$(arguments[0]).click()', treeGridPage)
        grid = Grid(browser, 'grid02', Selector.ID)
        # id =3 是lazyload模式，需要点击下拉
        grid.setValue('3', 'jqg1','jqg3', 'jqg6')
    finally:
        browser.close()

def test_tree_select_line_with_set_col_label():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/grid/index.html")
    time.sleep(2)
    try:
        treeGridPage = browser.find_element_by_id('treeGridPage.html')
        browser.execute_script('$(arguments[0]).click()', treeGridPage)
        grid = Grid(browser, 'grid02', Selector.ID)
        grid.setValue('name', 'test3', 'test3-child1', 'test3-child1-child1')
    finally:
        browser.close()

def test_wrong_selector():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/grid/index.html")
    time.sleep(2)
    try:
        multi_tree_grid = browser.find_element_by_id('multiSelect.html')
        browser.execute_script('$(arguments[0]).click()', multi_tree_grid)
        grid = Grid(browser, 'tabs-show', Selector.ID)
        with pytest.raises(TestException):
            grid.setValue('id', '29')
    finally:
        browser.close()


if __name__ == '__main__':
    pytest.main(['-s', 'Grid_test.py'])
